package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.VerificationCodeBean;
import com.lc.luzhengtang.conn.ConnBind;
import com.lc.luzhengtang.conn.ConnVerificationCode;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilMatches;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/11.
 */
public class ChangePhoneActivity2 extends BaseActivity {


    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_submit)
    TextView textSubmit;
    @BindView(R.id.edittext_nickName)
    EditText edittextNickName;
    @BindView(R.id.text_bind)
    TextView textBind;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.text_sendbtn)
    TextView textSendbtn;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.layout_bind)
    LinearLayout layoutBind;


    private PopupWindow popWindow;
    private TextView tv01, tv02, tv03;
    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                textSendbtn.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                textSendbtn.setText("重新获取");
                textSendbtn.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changephone2);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        edittextNickName.setText(BaseApplication.basePreferences.readMobile());
    }

    @OnClick({R.id.img_news, R.id.text_submit, R.id.edittext_nickName, R.id.text_bind, R.id.edit_code, R.id.text_sendbtn, R.id.btn_register, R.id.layout_bind})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_submit:
              /*  if (TextUtils.isEmpty(edittextNickName.getText().toString().trim())) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (edittextNickName.getText().toString().trim().length() < 6 || edittextNickName.getText().toString().trim().length() > 16) {

                    LovelyToast.makeText(ChangePhoneActivity2.this, "请输入正确的手机号码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(edittextNickName.getText().toString())) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    getChangeNickName();
                }*/


                break;
            case R.id.edittext_nickName:
                break;
            case R.id.text_bind:
                if (TextUtils.isEmpty(edittextNickName.getText().toString().trim())) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (edittextNickName.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(edittextNickName.getText().toString())) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    getChangeNickName();

                }


                break;
            case R.id.edit_code:
                break;
            case R.id.text_sendbtn:
                break;
            case R.id.btn_register:
                if (TextUtils.isEmpty(edittextNickName.getText().toString().trim())) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (edittextNickName.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(edittextNickName.getText().toString())) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    getChangeNickName();
                }

                break;

            case R.id.layout_bind:
                if (TextUtils.isEmpty(edittextNickName.getText().toString().trim())) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (edittextNickName.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(edittextNickName.getText().toString())) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    textSendbtn.setEnabled(false);
                    getVerificationCode();
                }
                break;
        }

    }


    public void getVerificationCode() {
        new ConnVerificationCode(edittextNickName.getText().toString().trim(), "shi", new AsyCallBack<VerificationCodeBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, VerificationCodeBean registerBean) throws Exception {

                LovelyToast.makeText(ChangePhoneActivity2.this, registerBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
//                btRegistered.setSelected(true);
                countTime.start();
                super.onSuccess(toast, type, o, registerBean);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
                textSendbtn.setEnabled(true);
                textSendbtn.setText("获取验证码");
                countTime.start();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
                countTime.start();
            }
        }


        ).execute(this);
    }

    public void getChangeNickName() {
        new ConnBind(edittextNickName.getText().toString().trim(), new AsyCallBack<CommenBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean changeNickName) throws Exception {
                super.onSuccess(toast, type, o, changeNickName);
                if (changeNickName.getCode() == 200) {
                    LovelyToast.makeText(ChangePhoneActivity2.this, changeNickName.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    BaseApplication.basePreferences.saveMobile(edittextNickName.getText().toString().trim());
                    finish();
//                    startActivity(new Intent(ChangePhoneActivity2.this, ChangeDataActivity.class));
                } else {
                    LovelyToast.makeText(ChangePhoneActivity2.this, changeNickName.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    BaseApplication.basePreferences.saveMobile(edittextNickName.getText().toString().trim());
                    finish();
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }

        ).execute(ChangePhoneActivity2.this, false);

    }


    private void showPopupWindow(View parent) {
        if (popWindow == null) {

            View view = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(this).inflate(R.layout.pop_settings_phone, null));
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public void initPop(View view) {
        tv01 = (TextView) view.findViewById(R.id.tv_01);//拍照
        tv02 = (TextView) view.findViewById(R.id.tv_02);//相册
        tv03 = (TextView) view.findViewById(R.id.tv_03);//取消
        LinearLayout layout_picselected = (LinearLayout) view.findViewById(R.id.layout_picselected);
        layout_picselected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });


        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popWindow.dismiss();
                startActivity(new Intent(ChangePhoneActivity2.this, YanzhengActivity.class));

            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(ChangePhoneActivity2.this, AppealActivity.class));

                popWindow.dismiss();

            }
        });
        tv03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popWindow.dismiss();
            }
        });
    }


}
